﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Base;

namespace HRM
{
    public partial class FormSelection : FormBaseDataGrid
    {
        private FormSearch frmSearch = null;
        private Ctrl_Selection mCtrlSelect;
        private SelectionParam mstrParam;
        private DataSet mdsSelect;

        public FormSelection()
        {
            InitializeComponent();
        }

        protected override void BasicForm_Load(object sender, EventArgs e)
        {
            base.BasicForm_Load(sender, e);
            foreach (DataColumn col in mdbg_dataGrid.Columns)
            {
                mdbg_dataGrid.Columns[col.ColumnName].SortMode = DataGridViewColumnSortMode.NotSortable;
                mdbg_dataGrid.Columns[col.ColumnName].ReadOnly = true;
            }
        }

        protected override void BaseForm_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            base.BaseForm_KeyDown(sender, e);
        }

        #region Override Load Master Data
        protected override void LoadMasterData()
        {
            if (mCtrlSelect == null) mCtrlSelect = new Ctrl_Selection();
            this.Text = " Selection list - [" + mstrParam.ms_SelectionName + "]";
            mdsSelect = mCtrlSelect.SetData2Selet(mfrmParentForm.Name, mstrParam.ms_SelectionField, mstrParam);
            if (mdsSelect != null)
            {
                mdbg_dataGrid.DataSource = mdsSelect.Tables[0];
                mRowManager = (CurrencyManager)mdbg_dataGrid.BindingContext[mdsSelect.Tables[0]];

                mdbg_dataGrid.Columns[mdbg_dataGrid.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                if (mdbg_dataGrid.Columns[0].HeaderText.Equals("Selection"))
                {
                    chkAll.Visible = true;
                    mdbg_dataGrid.Columns[0].Width = 60;
                }
            }
        }
        #endregion

        #region Properties set, get selection params
        public void SetParam(SelectionParam aps_param)
        { mstrParam = aps_param; }

        public SelectionParam GetParam()
        { return mstrParam; }
        #endregion

        private void SetReturnValue()
        {
            Cursor = Cursors.WaitCursor;
            SelectionParam lstr_param = new SelectionParam(); // create Structure to get values

            lstr_param.msa_ReturnValues = new string[4];
            switch (mfrmParentForm.Name)
            {
                case "FormPersonalProfile":
                    switch (mstrParam.ms_SelectionField)
                    {
                        case "txtIDPosition":
                        case "txtIDDept":
                        case "txtIDResideState":
                        case "txtIDResideCountry":
                        case "txtIDContState":
                        case "txtIDContCountry":
                        case "txtIDNative":
                        case "txtIDNationality":
                        case "txtIDRace":
                        case "txtIDLiteracy":
                        case "txtIDBank":
                        case "txtIDGLAccGroup":
                        case "ID_ContractTypeCont":
                            lstr_param.msa_ReturnValues = new string[2];
                            lstr_param.msa_ReturnValues[0] = mdbg_dataGrid[0, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                            lstr_param.msa_ReturnValues[1] = mdbg_dataGrid[1, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                            break;
                    } break;

                case "FormPlaceOfRegistedHI":
                case "FormHISIPIT_Info":
                case "FormDependent":
                case "FormBusinessTrip":
                case "FormCalculatePayroll":
                case "FormSalaryAdvance":
                case "FormMaternity":
                case "FormBonusFee":
                case "FormAllowanceFee":
                case "FormOvertimeRegistration":
                case "FormLeaveRegistration":
                case "FormManualTimeSheet":
                case "FormTraining":
                    switch (mstrParam.ms_SelectionField)
                    { 
                        case "ID_Emp":
                            lstr_param.msa_ReturnValues = new string[2];
                            for (int li_row = 0; li_row < mdbg_dataGrid.Rows.Count; li_row++)
                            {
                                if ((bool)mdbg_dataGrid["Selection", li_row].Value)
                                {
                                    if (lstr_param.msa_ReturnValues[0] == null || lstr_param.msa_ReturnValues[0].Length <= 0)
                                    {
                                        lstr_param.msa_ReturnValues[0] = mdbg_dataGrid[1, li_row].Value.ToString();
                                        lstr_param.msa_ReturnValues[1] = mdbg_dataGrid[2, li_row].Value.ToString();
                                    }
                                    else
                                    {
                                        lstr_param.msa_ReturnValues[0] = lstr_param.msa_ReturnValues[0] + "," + mdbg_dataGrid[1, li_row].Value.ToString();
                                        lstr_param.msa_ReturnValues[1] = lstr_param.msa_ReturnValues[1] + "," + mdbg_dataGrid[2, li_row].Value.ToString();
                                    }
                                }
                            }
                            if (lstr_param.msa_ReturnValues[0] == null || lstr_param.msa_ReturnValues[0].Length <= 0)
                            {
                                lstr_param.msa_ReturnValues[0] = "";
                                lstr_param.msa_ReturnValues[1] = "";
                            } break;

                        case "txtDept":
                        case "txtIDEmp":
                        case "ID_OvertimeType":
                        case "ID_LeaveType":
                        case "ID_Country":
                        case "ID_POR_HI":
                        case "ID_POR_Type":
                            lstr_param.msa_ReturnValues = new string[2];
                            lstr_param.msa_ReturnValues[0] = mdbg_dataGrid[0, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                            lstr_param.msa_ReturnValues[1] = mdbg_dataGrid[1, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                            break;
                    } break;

                        case "Form6002":
                        case "FormG0400":
                        case "FormG0100":
                        case "FormG0110":
                        case "FormG0120":
                            switch (mstrParam.ms_SelectionField)
                            {
                                case "Cost_Center_Code":
                                    lstr_param.msa_ReturnValues[0] = mdbg_dataGrid[0, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                                    lstr_param.msa_ReturnValues[1] = mdbg_dataGrid[1, mdbg_dataGrid.CurrentRow.Index].Value.ToString();
                                    break;
                            } break;

                        case "Another Form":
                            // add code here
                            break;
                    }
                    SetParam(lstr_param); // return values 
                    Cursor = Cursors.Default;
        }

        private void mdbg_dataGrid_DoubleClick(object sender, EventArgs e)
        {
            mdbg_dataGrid.EndEdit();
            SetReturnValue();
            this.DialogResult = DialogResult.OK;
        }

        private void btnSelect_Click(object sender, System.EventArgs e)
        {
            mdbg_dataGrid.EndEdit();
            SetReturnValue();
            this.DialogResult = DialogResult.OK;
        }

        protected override void mdbg_dataGrid_KeyUp(object sender, KeyEventArgs e)
        {
            base.mdbg_dataGrid_KeyUp(sender, e);
            if (e.KeyCode == Keys.Enter)
            {
                SetReturnValue();
                this.DialogResult = DialogResult.OK;
            }
        }

        private void chkAll_CheckedChanged(object sender, System.EventArgs e)
        {
            if (chkAll.Checked)
            {
                for (int li_row = 0; li_row < mdbg_dataGrid.Rows.Count; li_row++)
                {
                    mdbg_dataGrid["Selection",li_row].Value = true;
                }
            }
            else
            {
                for (int li_row = 0; li_row < mdbg_dataGrid.Rows.Count; li_row++)
                {
                    mdbg_dataGrid["Selection", li_row].Value = false;
                }
            }
        }

        private void mdbg_dataGrid_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            frmSearch = new FormSearch(this);
            frmSearch.Owner = this;
            frmSearch.Show();
        }

    }
}
